using System;
using System.IO;
using System.Collections;
using System.Text;
using System.Collections.Specialized;

namespace VCDepend.Engine.Graphviz
{
	public class DotAttributes : IDotSerializable
	{
		private StringDictionary m_attributes;
		private string m_prefix;
		private string m_separator;
		private string m_suffix;

    public DotAttributes(string prefix, string separator, string suffix)
		{
			m_attributes = new StringDictionary();
			m_prefix = prefix;
			m_separator = separator;
			m_suffix = suffix;
		}

		public void SetAttribute(string name, string value)
		{
			m_attributes[name] = value;
		}

		public void Serialize(TextWriter writer)
		{
			if (m_attributes.Count > 0)
			{
				writer.Write(m_prefix);

				int counter = 0;
				foreach (DictionaryEntry entry in m_attributes)
				{
					if (counter++ > 0)
						writer.Write(m_separator);

					string name = entry.Key.ToString();
					string value = entry.Value.ToString();
					writer.Write("{0}={1}", name, DotUtil.EscapeDotString(value));
				}

				writer.Write(m_suffix);
			}
		}
	}
}
